package com.maoxx.stuchoosemssbv_service.controller;

import com.maoxx.stuchoosemssbv_service.entity.Student;
import com.maoxx.stuchoosemssbv_service.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @className: StudentController
 * @author: Maoxx
 * @date: 2024/11/3 14:52
 * @Version: 1.0
 * @description:
 */


@RestController
@RequestMapping("/student")
@CrossOrigin(origins = "*")
public class StudentController {

    @Autowired
    private StudentService studentService;

    /**
     * 登录
     *
     * @param student 学生对象
     * @return boolean
     */
    @PostMapping("/login")
    public boolean login(@RequestBody Student student) {
        Student byId = studentService.findById(student.getSid());

        if (byId == null || !byId.getPassword().equals(student.getPassword())) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 根据id查询学生信息
     *
     * @param sid 学生id
     * @return 学生对象
     */
    @GetMapping("/findById/{sid}")
    public Student findById(@PathVariable Integer sid) {
        return studentService.findById(sid);
    }

    /**
     * 获取学生总数
     *
     * @return 学生总数
     */
    @GetMapping("/getLength")
    public Integer getLength() {
        return studentService.getLength();
    }

    /**
     * 分页查询学生信息
     * @param page 起始页
     * @param size 每页大小
     * @return 学生列表
     */
    @GetMapping("/findByPage/{page}/{size}")
    public List<Student> findByPage(@PathVariable Integer page, @PathVariable Integer size) {
        return studentService.findByPage(page, size);
    }

    /**
     * 根据条件查询学生信息
     * @param student 学生对象
     * @return 学生列表
     */
    @PostMapping("/findBySearch")
    public List<Student> findBySearch(@RequestBody Student student) {
        System.out.println("fuzzy:" + student.getPassword());
        // 检查该学生的密码是否为空
        Integer fuzzy = (student.getPassword() == null) ? 0 : 1;
        return studentService.findBySearch(student.getSid(), student.getSname(), fuzzy);
    }

    /**
     * 添加学生信息
     * @param student 学生对象
     * @return boolean
     */
    @PostMapping("/addStudent")
    public boolean addStudent(@RequestBody Student student) {
        return studentService.save(student);
    }

    /**
     * 修改学生信息
     * @param student 学生对象
     * @return boolean
     */
    @PutMapping("/updateStudent")
    public boolean updateStudent(@RequestBody Student student) {
        return studentService.updateById(student);
    }

    @DeleteMapping("/deleteById/{sid}")
    public boolean deleteById(@PathVariable("sid") Integer sid) {
        return studentService.deleteById(sid);
    }
}
